﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WAF.DAO;
using WAF.EntityFramework;

namespace EntityTools.Model
{
    public partial class DbInfo
    {
        /// <summary>
        /// 获取所有数据库信息
        /// </summary>
        /// <returns>数据库信息列表</returns>
        public static DbInfo[] GetDbList()
        {
            return EntityAction.Select<DbInfo>();
        }

        public static void AddDatabaseInfo()
        { 
            
        }

        /// <summary>
        /// 删除该数据库的所有信息（包括所有的表和字段信息）
        /// </summary>
        /// <param name="databaseID">数据库ID</param>
        public static void DeleteDatabaseInfo(long databaseID)
        {
            DbAccess dbAccess = new DbAccess();

            try
            {
                dbAccess.Open();
                dbAccess.BeginTransaction();

                DataTable tbInfos = DbAccess.ExeDataSet("SELECT Id FROM TableInfo WHERE DatabaseID=" + databaseID.ToString()).Tables[0];

                //删除该数据库中的表的字段信息
                foreach (DataRow row in tbInfos.Rows)
                {
                    dbAccess.ExecuteNonQuery("DELETE FROM FieldInfo WHERE TableID=" + row[0].ToString());
                }

                //删除该数据库中的表信息
                dbAccess.ExecuteNonQuery("DELETE FROM TableInfo WHERE DatabaseID=" + databaseID.ToString());

                //删除该数据库的信息
                dbAccess.ExecuteNonQuery("DELETE FROM DbInfo WHERE Id=" + databaseID.ToString());

                dbAccess.CommitTtransaction();
            }
            catch(Exception ex)
            {
                dbAccess.RollbackTtransaction();
                throw ex;
            }
            finally
            {
                dbAccess.Close();
            }
        }
    }
}
